#!/usr/bin/env bash
set -euo pipefail

# Usage: sudo ./deploy-nginx-config.sh <conf_file> [/etc/nginx/conf.d]

CONF_FILE=${1:-}
TARGET_DIR=${2:-/etc/nginx/conf.d}

if [[ -z "$CONF_FILE" || ! -f "$CONF_FILE" ]]; then
  echo "conf file not found: $CONF_FILE" >&2
  exit 1
fi

if [[ ! -d "$TARGET_DIR" ]]; then
  echo "target dir not found: $TARGET_DIR" >&2
  exit 1
fi

BASENAME=$(basename "$CONF_FILE")
TARGET_PATH="$TARGET_DIR/$BASENAME"

echo "Copying $CONF_FILE -> $TARGET_PATH"
cp "$CONF_FILE" "$TARGET_PATH"

echo "Testing nginx config"
nginx -t

echo "Reloading nginx"
systemctl reload nginx || nginx -s reload

echo "Done"

